/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
	/// <summary>
	/// Represents a Groove Contact.
	/// </summary>
	public class Contact : GrooveWebServicesV12HelpersBase
	{
		private GrooveWebServicesV12Helpers.Account m_Account;
		private GrooveWebServicesV12Helpers.VCard m_VCard;

		private GrooveContactsWebService.Contact m_Contact;
		private GrooveContactsWebService.GrooveContacts m_GrooveContacts;

		public Contact(GrooveWebServicesV12Helpers.Account i_Account, GrooveContactsWebService.Contact i_Contact)
			: base(i_Contact.Name, i_Contact.URI, i_Account.IdentityURL, i_Account.Context)
		{
			m_Account = i_Account;
			m_Contact = i_Contact;
		}

		#region Contact Properties And Methods

		public static GrooveContactsWebService.GrooveContacts CreateInitializedContactsWebService(string i_IdentityURL, string i_TargetURI, GrooveWebServicesV12Helpers.Context i_Context)
		{
			//  Create the service
			GrooveContactsWebService.GrooveContacts ContactsWebService = new GrooveContactsWebService.GrooveContacts();

			//  Create the Groove SOAP header
			ContactsWebService.GrooveRequestHeaderValue = new GrooveContactsWebService.GrooveRequestHeader();

			//  Set appropriate header values - note that any valid identity will suffice for obtaining contacts
			ContactsWebService.GrooveRequestHeaderValue.GrooveRequestKey = i_Context.RequestKey;
			ContactsWebService.GrooveRequestHeaderValue.GrooveIdentityURL = i_IdentityURL;

			//  Set URL
			ContactsWebService.Url = i_Context.Host + i_TargetURI;

			//  Set Timeout
			ContactsWebService.Timeout = i_Context.Timeout;

			return ContactsWebService;
		}

		/// <summary>
		/// The Account which contains this Contact.
		/// </summary>
		public GrooveWebServicesV12Helpers.Account Account
		{
			get
			{
				return m_Account;
			}
		}

		/// <summary>
		/// The VCard with this contact's details.
		/// </summary>
		public GrooveWebServicesV12Helpers.VCard VCard
		{
			get
			{
				//  Get initialized reference to web service
				GrooveVCardWebService.GrooveVCard VCardWebService = GrooveWebServicesV12Helpers.VCard.CreateInitializedVCardWebService
					(
					m_Account.IdentityURL,
					m_Contact.VCard,
					Context
					);

				System.DateTime BeginDateTime = System.DateTime.Now;

				//  Read
				GrooveWebServicesV12Helpers.GrooveVCardWebService.vCard VCard = VCardWebService.Read();

				if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
				{
					System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
					System.Diagnostics.Debug.WriteLine("VcardWebService::Read, " + TimeSpan.TotalMilliseconds + " ms");
				}

				m_VCard = new GrooveWebServicesV12Helpers.VCard(this, VCard);

				return m_VCard;
			}
		}

		/// <summary>
		/// The URI for raw web services access to this contact's VCard.
		/// </summary>
		public string VCardURI
		{
			get
			{
				return m_Contact.VCard;
			}
		}

		/// <summary>
		/// The awareness string for this contact.
		/// </summary>
		public string Awareness
		{
			get
			{
				return m_Contact.Awareness;
			}
		}

		/// <summary>
		/// If specified, the time this contact went idle.
		/// </summary>
		public System.DateTime TimeWentIdle
		{
			get
			{
				return m_Contact.TimeWentIdle;
			}
		}

		/// <summary>
		/// True if the TimeWentIdle is valid.
		/// </summary>
		public bool TimeWentIdleSpecified
		{
			get
			{
				return m_Contact.TimeWentIdleSpecified;
			}
		}

		/// <summary>
		/// The type of contact.
		/// </summary>
		public string Type
		{
			get
			{
				return m_Contact.Type;
			}
		}

		private GrooveContactsWebService.GrooveContacts GetInitializedContactsWebService(string i_TargetURI)
		{
            if ((m_GrooveContacts == null) || Context.ReinitNeeded(m_GrooveContacts.GrooveRequestHeaderValue.GrooveRequestKey))
			{
				m_GrooveContacts = GrooveWebServicesV12Helpers.Contact.CreateInitializedContactsWebService
					(
					m_Account.IdentityURL,
					i_TargetURI,
					Context
					);
			}
			else
			{
				//  Explicitly overwrite target URL and Timeout
				m_GrooveContacts.Url = Context.Host + i_TargetURI;
                m_GrooveContacts.Timeout = Context.Timeout;
			}

			return m_GrooveContacts;
		}

		#endregion

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
		{
			get
			{
				if (System.String.IsNullOrEmpty(m_BindableURL))
				{
					//  Sample Contact URI is 'grooveIdentity://wim97jf4m6v9j6bugjnk57v54xgmkunt@', so we strip
					//  off 'grooveIdentity://'
					string ContactInfo = this.URI;
					int Index = ContactInfo.LastIndexOf("/") + 1;
					ContactInfo = ContactInfo.Substring(Index);

					m_BindableURL = m_Account.BindableURL + "/Contact#" + ContactInfo;			
				}

			return m_BindableURL;
			}
		}

		#endregion
	}
}
